package taquin;

import java.awt.EventQueue;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.Button;
import javax.swing.JTable;
import java.awt.FlowLayout;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.Color;
import javax.swing.ImageIcon;
import java.awt.Font;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import javax.swing.Action;
import java.awt.event.ActionListener;
import javax.swing.JList;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Image;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Panel;

import javax.swing.JTextArea;
import java.awt.SystemColor;
import javax.swing.border.LineBorder;
import java.awt.CardLayout;
import java.io.File;
import java.io.IOException;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.BoxLayout;

public class iii extends JFrame{
	private Image img;
    private JFrame frame;
	int posZero=0;


	/**
	 * Launch the application.
	 */
	
	/*public  iii(Image img) {
	    this.img = img;
	    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
	    setPreferredSize(size);
	    setMinimumSize(size);
	    setMaximumSize(size);
	    setSize(size);
	    setLayout(null);
	  }
	 */
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					iii window = new iii();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public iii() {
		
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		
		
		
		
		
		frame = new JFrame();
		frame.getContentPane().setBackground(SystemColor.textText);
		frame.setBounds(100, 100, 654, 488);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		final JButton button_0 = new JButton("0");
		final JButton button_1 = new JButton("1");
		final JButton button_2 = new JButton("2");
		final JButton button_3 = new JButton("3");
        final JButton button_4 = new JButton("4");
		final JButton button_5 = new JButton("5");
		final JButton button_7 = new JButton("7");
		final JButton button_6 = new JButton("6");
        final JButton button_8 = new JButton("8");
        final JTextArea txtrBbbbb = new JTextArea();
		txtrBbbbb.setBounds(299, 130, 262, 114);
		
		final JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "25", "26", "27", "28", "29", "30"}));
		comboBox.setToolTipText("1");
		comboBox.setBounds(435, 321, 45, 20);
		
		final JLabel lblTempsDeResolution = new JLabel("Temps de resolution :");
		lblTempsDeResolution.setForeground(new Color(0, 0, 128));
		lblTempsDeResolution.setBounds(299, 61, 136, 14);
		
		final JLabel lblSecondes = new JLabel("secondes");
		lblSecondes.setForeground(new Color(0, 0, 128));
		lblSecondes.setBounds(435, 61, 142, 14);
		
		
		button_2.setFont(new Font("Tahoma", Font.PLAIN, 20));
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String val=button_2.getText();
				if(posZero==1){
				    button_2.setText("0");
				    button_2.setVisible(false);
				    button_1.setText(val);
				    button_1.setVisible(true);
				    posZero=2;}
				else if(posZero==5){
					button_2.setText("0");
				    button_2.setVisible(false);
				    button_5.setText(val);
				    button_5.setVisible(true);
				    posZero=2;
				}
			}
		});
		button_2.setBounds(149, 38, 45, 37);
		frame.getContentPane().add(button_2);
		

		
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String val=button_4.getText();
				if(posZero==1){
				    button_4.setText("0");
				    button_4.setVisible(false);
				    button_1.setText(val);
				    button_1.setVisible(true);
				    posZero=4;
}
				else if(posZero==3){
					button_4.setText("0");
				    button_4.setVisible(false);
				    button_3.setText(val);
				    button_3.setVisible(true);
				    posZero=4;

				}else if(posZero==5){
					button_4.setText("0");
				    button_4.setVisible(false);
				    button_5.setText(val);
				    button_5.setVisible(true);
				    posZero=4;

				}
				else if(posZero==7){
					button_4.setText("0");
				    button_4.setVisible(false);
				    button_7.setText(val);
				    button_7.setVisible(true);
				    posZero=4;

				}
			}
		});
		button_4.setFont(new Font("Tahoma", Font.PLAIN, 20));
		button_4.setBounds(94, 86, 45, 37);
		frame.getContentPane().add(button_4);
		
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String val=button_1.getText();

				if(posZero==0){
				    button_1.setText("0");
				    button_1.setVisible(false);
				    button_0.setText(val);
				    button_0.setVisible(true);
				    posZero=1;
}
				else if(posZero==2){
					button_1.setText("0");
				    button_1.setVisible(false);
				    button_2.setText(val);
				    button_2.setVisible(true);
				    posZero=1;

				}else if(posZero==4){
					button_1.setText("0");
				    button_1.setVisible(false);
				    button_4.setText(val);
				    button_4.setVisible(true);
				    posZero=1;

				}

			}
		});
		button_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
		button_1.setBounds(94, 38, 45, 37);
	//	button_1.setVisible(false);

		frame.getContentPane().add(button_1);
		
		
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String val=button_5.getText();

				if(posZero==2){
				    button_5.setText("0");
				    button_5.setVisible(false);
				    button_2.setText(val);
				    button_2.setVisible(true);
				    posZero=5;
}
				else if(posZero==4){
					button_5.setText("0");
				    button_5.setVisible(false);
				    button_4.setText(val);
				    button_4.setVisible(true);
				    posZero=5;

				}else if(posZero==8){
					button_5.setText("0");
				    button_5.setVisible(false);
				    button_8.setText(val);
				    button_8.setVisible(true);
				    posZero=5;

				}
			}
		});
		button_5.setFont(new Font("Tahoma", Font.PLAIN, 20));
		button_5.setBounds(149, 86, 45, 37);
		frame.getContentPane().add(button_5);
		
		button_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String val=button_8.getText();

				if(posZero==5){
				    button_8.setText("0");
				    button_8.setVisible(false);
				    button_5.setText(val);
				    button_5.setVisible(true);
				    posZero=8;}
				else if(posZero==7){
					button_8.setText("0");
				    button_8.setVisible(false);
				    button_7.setText(val);
				    button_7.setVisible(true);
				    posZero=8;
				}
			}
		});
		button_8.setFont(new Font("Tahoma", Font.PLAIN, 20));
		button_8.setBounds(149, 134, 45, 37);
		frame.getContentPane().add(button_8);
		
		button_7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String val=button_7.getText();

				if(posZero==4){
				    button_7.setText("0");
				    button_7.setVisible(false);
				    button_4.setText(val);
				    button_4.setVisible(true);
				    posZero=7;}
				else if(posZero==6){
					button_7.setText("0");
				    button_7.setVisible(false);
				    button_6.setText(val);
				    button_6.setVisible(true);
				    posZero=7;
				}else if(posZero==8){
					button_7.setText("0");
				    button_7.setVisible(false);
				    button_8.setText(val);
				    button_8.setVisible(true);
				    posZero=7;
				}
			}
		});
		button_7.setFont(new Font("Tahoma", Font.PLAIN, 20));
		button_7.setBounds(94, 134, 45, 37);
		frame.getContentPane().add(button_7);
		
		button_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String val=button_6.getText();

				if(posZero==3){
				    button_6.setText("0");
				    button_6.setVisible(false);
				    button_3.setText(val);
				    button_3.setVisible(true);
				    posZero=6;}
				else if(posZero==7){
					button_6.setText("0");
				    button_6.setVisible(false);
				    button_7.setText(val);
				    button_7.setVisible(true);
				    posZero=6;
				}
			}
		});
		button_6.setFont(new Font("Tahoma", Font.PLAIN, 20));
		button_6.setBounds(39, 134, 45, 37);
		frame.getContentPane().add(button_6);
		
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String val=button_3.getText();

				if(posZero==0){
				    button_3.setText("0");
				    button_3.setVisible(false);
				    button_0.setText(val);
				    button_0.setVisible(true);
				    posZero=3;
}
				else if(posZero==4){
					button_3.setText("0");
				    button_3.setVisible(false);
				    button_4.setText(val);
				    button_4.setVisible(true);
				    posZero=3;

				}else if(posZero==6){
					button_3.setText("0");
				    button_3.setVisible(false);
				    button_6.setText(val);
				    button_6.setVisible(true);
				    posZero=3;

				}
			}
		});
		button_3.setFont(new Font("Tahoma", Font.PLAIN, 20));
		button_3.setBounds(39, 86, 45, 37);
		frame.getContentPane().add(button_3);
		
		button_0.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String val=button_0.getText();

				if(posZero==1){
				    button_0.setText("0");
				    button_0.setVisible(false);
				    button_1.setText(val);
				    button_1.setVisible(true);
				    posZero=0;
}
				else if(posZero==3){
					button_0.setText("0");
				    button_0.setVisible(false);
				    button_3.setText(val);
				    button_3.setVisible(true);
				    posZero=0;

				}else if(posZero==4){
					button_0.setText("0");
				    button_0.setVisible(false);
				    button_4.setText(val);
				    button_4.setVisible(true);
				    posZero=0;

				}
			}
		});
		button_0.setFont(new Font("Tahoma", Font.PLAIN, 20));
		button_0.setBounds(39, 38, 45, 37);
		button_0.setVisible(false);
		frame.getContentPane().add(button_0);
		
		JButton btnPositionInitial = new JButton("A*");
		btnPositionInitial.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				/*button_0.setText("0");
				button_0.setVisible(false);
				posZero=0;
				button_1.setText("1");
				button_1.setVisible(true);
             	button_2.setText("2");
				button_2.setVisible(true);

				button_3.setText("3");
				button_3.setVisible(true);

				button_4.setText("4");
				button_4.setVisible(true);

				button_5.setText("5");
				button_5.setVisible(true);

				button_6.setText("6");
				button_6.setVisible(true);

				button_7.setText("7");
				button_7.setVisible(true);

				button_8.setText("8");
				button_8.setVisible(true);
				lblTempsDeResolution.setVisible(false);
                lblSecondes.setVisible(false);
                txtrBbbbb.setText("");
                comboBox.setSelectedIndex(0);*/
				int i0=Integer.parseInt(button_0.getText());
				int i1=Integer.parseInt(button_1.getText());
				int i2=Integer.parseInt(button_2.getText());
				int i3=Integer.parseInt(button_3.getText());
				int i4=Integer.parseInt(button_4.getText());
				int i5=Integer.parseInt(button_5.getText());
				int i6=Integer.parseInt(button_6.getText());
				int i7=Integer.parseInt(button_7.getText());
				int i8=Integer.parseInt(button_8.getText());
				int niveau =comboBox.getSelectedIndex();
                Taquin t=new Taquin(i0, i1, i2, i3, i4, i5, i6, i7, i8, "0",10);
                String sol=null;
                //pour le calcule de temps dans l'operation de recherche de sol
                long start; 
        		start = System.nanoTime();
                //---
                
                try {
					 sol=Resolution.construireAetoile(t, niveau);
				} catch (CloneNotSupportedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//---
				long duree = System.nanoTime() - start;
		        float time=(float)duree/100000000;
		        //--

				
				String  Solution ="";
				for(int i=1;i<sol.length();i++){
					if(i % 5 ==0)Solution+="\n";
					if (sol.substring(i, i+1).compareTo("d")==0) Solution+="-->Droite";
					if (sol.substring(i, i+1).compareTo("g")==0) Solution+="-->Gauche";
					if (sol.substring(i, i+1).compareTo("h")==0) Solution+="-->Haut";
					if (sol.substring(i, i+1).compareTo("b")==0) Solution+="-->Bas";

				}
				if (Solution=="") 				txtrBbbbb.setText("Desole mais il ya pas de solution");
				else {
				txtrBbbbb.setText("La solution :\n"+Solution);
				lblTempsDeResolution.setVisible(true);
				lblSecondes.setText(""+time+" secondes");
				lblSecondes.setVisible(true);
				}
			}

			
		});
		btnPositionInitial.setBounds(39, 257, 206, 23);
		frame.getContentPane().add(btnPositionInitial);
		
		JButton btnResolutionLargeur = new JButton("Resolution en largeur");
		btnResolutionLargeur.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				int i0=Integer.parseInt(button_0.getText());
				int i1=Integer.parseInt(button_1.getText());
				int i2=Integer.parseInt(button_2.getText());
				int i3=Integer.parseInt(button_3.getText());
				int i4=Integer.parseInt(button_4.getText());
				int i5=Integer.parseInt(button_5.getText());
				int i6=Integer.parseInt(button_6.getText());
				int i7=Integer.parseInt(button_7.getText());
				int i8=Integer.parseInt(button_8.getText());
				int niveau =comboBox.getSelectedIndex();
                Taquin t=new Taquin(i0, i1, i2, i3, i4, i5, i6, i7, i8, "0",10);
                String sol=null;
                //pour le calcule de temps dans l'operation de recherche de sol
                long start; 
        		start = System.nanoTime();
                //---
                
                try {
					 sol=Resolution.construireLargeur(t, niveau);
				} catch (CloneNotSupportedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//---
				long duree = System.nanoTime() - start;
		        float time=(float)duree/100000000;
		        //--

				
				String  Solution ="";
				for(int i=1;i<sol.length();i++){
					if(i % 5 ==0)Solution+="\n";
					if (sol.substring(i, i+1).compareTo("d")==0) Solution+="-->Droite";
					if (sol.substring(i, i+1).compareTo("g")==0) Solution+="-->Gauche";
					if (sol.substring(i, i+1).compareTo("h")==0) Solution+="-->Haut";
					if (sol.substring(i, i+1).compareTo("b")==0) Solution+="-->Bas";

				}
				if (Solution=="") 				txtrBbbbb.setText("Desole mais il ya pas de solution");
				else {
				txtrBbbbb.setText("La solution :\n"+Solution);
				lblTempsDeResolution.setVisible(true);
				lblSecondes.setText(""+time+" secondes");
				lblSecondes.setVisible(true);
				}
			}
		});
		btnResolutionLargeur.setBounds(39, 301, 206, 23);
		frame.getContentPane().add(btnResolutionLargeur);
		
		
		
		frame.getContentPane().add(txtrBbbbb);
		
		
		frame.getContentPane().add(comboBox);
		
		JLabel lblChoisirLeNiveau = new JLabel("Choisir le niveau");
		lblChoisirLeNiveau.setForeground(new Color(0, 0, 128));
		lblChoisirLeNiveau.setBounds(304, 322, 121, 18);
		frame.getContentPane().add(lblChoisirLeNiveau);
		
		JButton btnResolutionProfondeur = new JButton("Resolution en profondeur");
		btnResolutionProfondeur.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int i0=Integer.parseInt(button_0.getText());
				int i1=Integer.parseInt(button_1.getText());
				int i2=Integer.parseInt(button_2.getText());
				int i3=Integer.parseInt(button_3.getText());
				int i4=Integer.parseInt(button_4.getText());
				int i5=Integer.parseInt(button_5.getText());
				int i6=Integer.parseInt(button_6.getText());
				int i7=Integer.parseInt(button_7.getText());
				int i8=Integer.parseInt(button_8.getText());
				int niveau =comboBox.getSelectedIndex();
                Taquin t=new Taquin(i0, i1, i2, i3, i4, i5, i6, i7, i8, "0",10);
                String sol=null;
                //pour le calcule de temps dans l'operation de recherche de sol
                long start; 
        		start = System.nanoTime();
                //---
                
                try {
					 sol=Resolution.construireProfondeur(t, niveau);
				} catch (CloneNotSupportedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//---
				long duree = System.nanoTime() - start;
		        float time=(float)duree/100000000;
		        //--

				
				String  Solution ="";
				for(int i=1;i<sol.length();i++){
					if (sol.substring(i, i+1).compareTo("d")==0) Solution+="-->Droite";
					if (sol.substring(i, i+1).compareTo("g")==0) Solution+="-->Gauche";
					if (sol.substring(i, i+1).compareTo("h")==0) Solution+="-->Haut";
					if (sol.substring(i, i+1).compareTo("b")==0) Solution+="-->Bas";

				}
				if (Solution=="") 				txtrBbbbb.setText("Desole mais il ya pas de solution");
				else {
				txtrBbbbb.setText("La solution :\n"+Solution);
				lblTempsDeResolution.setVisible(true);
				lblSecondes.setText(""+time+" secondes");
				lblSecondes.setVisible(true);
				}
			}
		});
		btnResolutionProfondeur.setBounds(39, 345, 206, 23);
		frame.getContentPane().add(btnResolutionProfondeur);
		
		lblTempsDeResolution.setVisible(false);
		frame.getContentPane().add(lblTempsDeResolution);
		
        lblSecondes.setVisible(false);
		frame.getContentPane().add(lblSecondes);
		
		//Image fond;
		/*try {
			fond = ImageIO.read(new File("fond.jpg"));


	 	panel panel=new panel(fond);
	 	this.setContentPane(panel);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		Image fond = null;
		try {
			fond = ImageIO.read(new File("taquin.jpg"));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		JPanel panel_1 = new panel(fond);
		panel_1.setBounds(0, 0, 638, 452);
		frame.getContentPane().add(panel_1);
		panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
		
		JButton button = new JButton("position initial");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				button_0.setText("0");
				button_0.setVisible(false);
				posZero=0;
				button_1.setText("1");
				button_1.setVisible(true);
             	button_2.setText("2");
				button_2.setVisible(true);

				button_3.setText("3");
				button_3.setVisible(true);

				button_4.setText("4");
				button_4.setVisible(true);

				button_5.setText("5");
				button_5.setVisible(true);

				button_6.setText("6");
				button_6.setVisible(true);

				button_7.setText("7");
				button_7.setVisible(true);

				button_8.setText("8");
				button_8.setVisible(true);
				lblTempsDeResolution.setVisible(false);
                lblSecondes.setVisible(false);
                txtrBbbbb.setText("");
                comboBox.setSelectedIndex(0);
			}
		});
		panel_1.add(button);
		
		
	/*	//Image fond;
		panel panel;
		try {
			
			
			fond = ImageIO.read(new File("taquin.jpg"));

	 	// panel=new panel(fond);
	 	//this.setContentPane(panel);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
	/*	
		panel.setBackground(Color.DARK_GRAY);
		panel.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel.setBounds(0, 0, 557, 307);
		frame.getContentPane().add(panel);
		panel.setLayout(new CardLayout(0, 0));*/
	}
}



